package com.tjoydoorsystem.bluetooth;

import static android.content.Context.BIND_AUTO_CREATE;

import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.tjoydoorsystem.Global;
import com.tjoydoorsystem.util.TextUtil;

import java.util.Arrays;
import java.util.UUID;

/**
 * Created by hebin
 * on 2017/2/27 0027.
 */
@SuppressWarnings("ALL")
public class BluetoothUtil {

    private static final String TAG = "BluetoothUtil";

    private static BluetoothLeService mBluetoothLeService;

    private Context context;

    private BluetoothUtil() {

    }

    private static BluetoothUtil bluetoothUtil = null;

    public static synchronized BluetoothUtil getInstance() {
        if (bluetoothUtil == null) {
            bluetoothUtil = new BluetoothUtil();
        }
        return bluetoothUtil;
    }

    /**
     * 初始化蓝牙连接
     */
    public void initBluetooth(final Context context) {
        this.context = context;

        //蓝牙服务
        Intent gattServiceIntent = new Intent(context, BluetoothLeService.class);

        // 服务绑定监听
        ServiceConnection mServiceConnection = new ServiceConnection() {

            @Override
            public void onServiceConnected(ComponentName componentName, IBinder service) {

                mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();

                if (!mBluetoothLeService.initialize()) {
                    Log.e(TAG, "Unable to initialize Bluetooth");
                    return;
                }
                Log.i(TAG, "蓝牙设备链接开始");
                //                Toast.makeText(context,"开始链接蓝牙设备",Toast.LENGTH_SHORT).show();
                mBluetoothLeService.connect(Global.Const.MAC);
            }


            @Override
            public void onServiceDisconnected(ComponentName componentName) {
                mBluetoothLeService = null;
            }
        };

        //start Sservice需要注册  绑定不用  广播注册需要到 xml中注册
        Log.i(TAG, "蓝牙服务绑定");
        //        Toast.makeText(context,"开始绑定蓝牙服务",Toast.LENGTH_SHORT).show();
        boolean bll = context.bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);

        if (bll) {
            Toast.makeText(context, "蓝牙服务绑定服务成功", Toast.LENGTH_LONG).show();
            Log.i(TAG, "蓝牙服务绑定服务成功");

        } else {
            Toast.makeText(context, "蓝牙服务绑定服务失败", Toast.LENGTH_LONG).show();
            Log.i(TAG, "蓝牙服务绑定服务失败");
        }
    }

    /**
     * 向设备输入指令
     */
    public void writeData(byte[] data) {

        if (mBluetoothLeService == null) {
            Toast.makeText(context, "蓝牙服务未连接", Toast.LENGTH_LONG).show();
            Log.e(TAG, "蓝牙服务未连接");
            //重新连接
            BluetoothUtil.getInstance().initBluetooth(context);
            return;
        }

        if (mBluetoothLeService.getSupportedGattServices() != null) {
            for (BluetoothGattService service : mBluetoothLeService.getSupportedGattServices()) {
                if (service.getUuid().toString().equals("0000ffe0-0000-1000-8000-00805f9b34fb")) {
                    BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString("0000ffe1-0000-1000-8000-00805f9b34fb"));
                    if (!TextUtil.isEmpty(Arrays.toString(data))) {
                        characteristic.setValue(data);
                        mBluetoothLeService.wirteCharacteristic(characteristic);
                        Log.i(TAG, "写入设备特征值: characteristic  =" + Arrays.toString(characteristic.getValue()));
                        Log.i(TAG, "写入服务地址: service  =" + characteristic.getService().getUuid());
                        Log.i(TAG, "写入特征地址: characteristic  = " + characteristic.getUuid());
                    }
                }
            }
        } else {
            Toast.makeText(context, "设备服务列表为空", Toast.LENGTH_LONG).show();
            Log.i(TAG, "设备服务列表为空");
        }
    }

}
